﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MVC3DEMO.Models;
using MVC3DEMO.Models.BS;
using System.Drawing;


namespace MVC3DEMO.Controllers
{
    public class FileStoreController : Controller
    {
        private SQLHeperDataContext sqlheper;
        private BS_FileStore bs_fileStore;
       
        public FileStoreController()
        {
            sqlheper = new SQLHeperDataContext();
            bs_fileStore = new BS_FileStore();
            
        }
        //
        // GET: /FileStore/

        public ActionResult Index()
        {
            foreach (string upload in Request.Files)
            {


                string mimeType = Request.Files[upload].ContentType;
                Stream fileStream = Request.Files[upload].InputStream;
                string fileName = Path.GetFileName(Request.Files[upload].FileName);
                int fileLength = Request.Files[upload].ContentLength;
                byte[] fileData = new byte[fileLength];
                fileStream.Read(fileData, 0, fileLength);
                sqlheper.asp_InsertFileStore(fileData, mimeType, fileName);

            }
            return View();
        }

        //
        // GET: /FileStore/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /FileStore/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FileStore/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /FileStore/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /FileStore/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /FileStore/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /FileStore/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult ViewVideo()
        {
            var listvieo = bs_fileStore.getListFile();
            List<FileStore> list = new List<FileStore>();
            foreach (FileStore f in listvieo)
            {
                byte[] bs = (byte[])f.FileContent.ToArray();
                MemoryStream ms = new MemoryStream(bs);
                FileStore fs = new FileStore();

                fs.FileName = f.FileName;
                fs.MimeType = f.MimeType;
                list.Add(fs);

                Response.Write(Image.FromStream(ms));
            }
            return View(list);
        }



        public ActionResult jwplayer()
        {
            return View();
        }


    }
}
